<?php
// +----------------------------------------------------------------------
// | Copyright (c) 2017~2023 https://www.gxgyl.com All rights reserved.

// +----------------------------------------------------------------------
// | Author: Liuyu
// +----------------------------------------------------------------------
declare (strict_types = 1);

namespace app\store\model\shiyao;

use app\common\model\shiyao\PayImage as PayImageModel;

/**
 * 申办方模型
 * Class StoreUser
 * @package app\store\model
 */
class PayImage extends PayImageModel {

	public function getListAll() {
		// 查询模型
		$query = $this->getNewQuery();

		// 查询列表记录
		$list = $query
			->where('is_delete', '=', '0')
			->order(['sort' => 'asc', 'create_time' => 'desc'])
			->select()->toArray();

		return $list;
	}

	public function getList($param = []) {
		// 查询模型
		$query = $this->getNewQuery();
		// 查询参数
		$params = $this->setQueryDefaultValue($param, ['centerId' => '', 'search' => '', 'betweenTime' => [], 'payStatus' => -1]);
		$filter=[];
		// 起止时间
		if (!empty($params['betweenTime'])) {
			$times = between_time($params['betweenTime']);
			$filter[] = ['p.create_time', '>=', $times['start_time']];
			$filter[] = ['p.create_time', '<', $times['end_time'] + 86400];
		}
		if ($params['payStatus'] > 0) {
			$filter[] = ['p.pay_status', '=', $params['payStatus']];
		} else {
			//$filter[] = ['p.pay_status', '>', 0];
		}

		!empty($params['centerId']) && $query->where('p.center_id', '=', $params['centerId']);

		// 关键词搜索
		!empty($params['search']) && $query->where('pr.project_name|c.company_name|center.center_code', 'like', "%{$params['search']}%");
		// 查询列表记录
		$list = $query->alias('p')->with(['center', 'project.company', 'testee','images','openImages'])
			->join('shiyao_project pr', 'pr.project_id=p.project_id')
			->join('shiyao_company c', 'c.company_id=pr.company_id')
			->join('shiyao_project_center center', 'center.id=p.center_id')
			->field('p.*')
			->where($filter)
			->where('p.is_delete', '=', '0')
			->order(['p.create_time' => 'desc'])
			->paginate(15);

		return $list;
	}

	/**
	 * 新增记录
	 * @param array $data
	 * @return bool
	 */
	public function add(array $data) {
		$data['PayLog_name'] = strtolower($data['PayLog_name']);

		$data['store_id'] = self::$storeId;

		// 事务处理
		$this->transaction(function () use ($data) {
			// 新增
			$this->save($data);

		});
		return true;
	}

	/**
	 * 更新记录
	 * @param array $data
	 * @return bool
	 */
	public function edit(array $data) {
		$data['PayLog_name'] = strtolower($data['PayLog_name']);

		$this->transaction(function () use ($data) {
			// 更新记录
			$this->save($data);

		});
		return true;
	}

	public function audit(array $data) {

		$this->transaction(function () use ($data) {
			// 更新记录
			$this->save($data);

		});
		return true;
	}

	/**
	 * 软删除
	 * @return false|int
	 */
	public function setDelete() {

		return $this->transaction(function () {
			// 删除对应的角色关系
			UserRole::deleteAll([['store_user_id', '=', (int) $this['store_user_id']]]);
			return $this->save(['is_delete' => 1]);
		});
	}
	public static function detail(int $PayLogId) {

		$detail = self::get(['PayLog_id' => $PayLogId]);

		return $detail;
	}

}
